package com.example.dense_rack_server.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.dense_rack_server.entity.Host;
import com.example.dense_rack_server.entity.Notice;
import com.example.dense_rack_server.mapper.HostMapper;
import com.example.dense_rack_server.mapper.NoticeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dense_rack_server.utils.Netty.SendCmd;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公告下发 服务实现类
 * </p>
 *
 * @author  张彬 
 * @since 2025-03-28
 */
@Service
public class NoticeService extends ServiceImpl<NoticeMapper, Notice> {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    public SendCmd sendCmd;
    @Resource
    public HostMapper hostMapper;

    /**
     * 公告表格
     */
    public Map<String, Object> listTable(String hostId, int page, int limit) {
        Map<String, Object> paramsMap = new HashMap<>();
        int offset = (page - 1) * limit;
        paramsMap.put("page", offset);
        paramsMap.put("limit", limit);
        if (StrUtil.isNotEmpty(hostId)) {
            paramsMap.put("hostId", hostId);
        }
        List<Map<String, Object>> mapList = noticeMapper.selectNoticeList(paramsMap);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", noticeMapper.selectNoticeListCount(paramsMap));
        map.put("data", mapList);
        return map;
    }

    /**
     * 公告修改
     */
    public ResponseModel updateSave(int noticeId, String noticeContent,Integer syncFunction) throws InterruptedException {
        Notice notice = noticeMapper.selectById(noticeId);
        if (notice == null) {
            return ResponseModel.fail("公告不存在");
        }
        // 发送通知
        Host host = hostMapper.selectById(notice.getHostId());
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("type", "notice");
        paramsMap.put("notice", noticeContent);
        paramsMap.put("lie", notice.getColumnNumber());
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        if (syncFunction == 1) {
            // 同步通知
            for (int i = 1; i <= host.getColumnNumber(); i++) {
                paramsMap.put("lie", i);
                String noticeStrMap = JSONUtil.toJsonStr(paramsMap);
                sendCmd.send(noticeStrMap, null, host.getIp());
                Thread.sleep(100);
            }
            updateWrapper.eq("host_id", host.getId());
            updateWrapper.set("notice_content", noticeContent);
            int count = noticeMapper.update(null, updateWrapper);
            return count > 0 ? ResponseModel.success("修改成功") : ResponseModel.fail("修改失败");
        } else {
            String noticeStrMap = JSONUtil.toJsonStr(paramsMap);
            sendCmd.send(noticeStrMap, null, host.getIp());
            updateWrapper.eq("id", noticeId);
            updateWrapper.set("notice_content", noticeContent);
            int count = noticeMapper.update(null, updateWrapper);
            return count > 0 ? ResponseModel.success("修改成功") : ResponseModel.fail("修改失败");

        }





    }
}
